//
//  SearchNavigationView.m
//  搜索功能
//
//  Created by Hello Cai on 2020/10/10.
//  Copyright © 2020 Hello Cai. All rights reserved.
//

#import "SearchNavigationView.h"

@interface SearchNavigationView ()<UITextFieldDelegate>
@property(nonatomic, strong)UIButton *backBtn;
@property(nonatomic, strong)UIButton *cancelBtn;
@property(nonatomic, assign)BOOL didSearched;
@end

@implementation SearchNavigationView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setView];
    }
    return self;
}

-(void)setView{
    
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 0, 35, self.height)];
    backBtn.hidden = YES;
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"ic_back"] forState:UIControlStateNormal];
    [self addSubview:backBtn];
    self.backBtn = backBtn;
    
    _searchTF = [[UITextField alloc] initWithFrame:CGRectMake(15, 4, kScreenW-15-64, 35)];
    _searchTF.borderStyle = UITextBorderStyleNone;
    _searchTF.returnKeyType = UIReturnKeySearch;
    _searchTF.backgroundColor = RGB(243, 244, 245);
    _searchTF.layer.cornerRadius = 15;
    _searchTF.layer.masksToBounds = YES;
    _searchTF.placeholder = @"搜索商品链接/商品名称";
    _searchTF.font = [UIFont systemFontOfSize:15];
    _searchTF.delegate = self;
    _searchTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self addSubview:_searchTF];
    [_searchTF becomeFirstResponder];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 0)];
    _searchTF.leftView = leftView;
    _searchTF.leftViewMode = UITextFieldViewModeAlways;
    
    
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenW-64, 0, 64, self.frame.size.height)];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
    self.cancelBtn = cancelBtn;
}

#pragma mark UITextFieldDelegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    NSLog(@"%s", __func__);
    
    [self modifyFrameWithEiding:YES];
    if (self.editingTFBlock) {
        self.editingTFBlock(YES);
    }
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    // 空格处理
    NSString *text = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"%@", text);
    if (text == nil || [text isEqualToString:@""]) {
        return YES;
    }
    
    [textField resignFirstResponder];
    
    [self modifyFrameWithEiding:NO];
    if (self.searchTextBlock) {
        self.searchTextBlock(text);
    }
    self.didSearched = YES;
    return YES;
}

-(void)modifyFrameWithEiding:(BOOL)editing{
    if (editing == YES) {
        self.searchTF.frame = CGRectMake(15, 4, kScreenW-15-64, 35);
        self.backBtn.hidden = YES;
        self.cancelBtn.hidden = NO; //右边带取消按钮
    }else{
        self.searchTF.frame = CGRectMake(50, 4, kScreenW-50-30, 35);
        self.backBtn.hidden = NO;   //左边带返回按钮
        self.cancelBtn.hidden = YES;
    }
}

-(void)searchText:(NSString *)text{
    self.didSearched = YES;
    self.searchTF.text = text;
    [self modifyFrameWithEiding:NO];
    [self.searchTF resignFirstResponder];
}

//取消按钮
-(void)cancelClick{
    [self.searchTF resignFirstResponder];
    if (self.didSearched) {
        [self modifyFrameWithEiding:NO];
        if (self.editingTFBlock) {
            self.editingTFBlock(NO);
        }
    }else{
        [self backClick];
    }
}

//返回按钮
-(void)backClick{
    if (self.navBackBlock) {
        self.navBackBlock();
    }
}

@end
